﻿using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Options;
using MongoDB.Bson.Serialization.Serializers;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Base.Common
{
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：BaseConfig                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2020/9/16 13:39:34                        
   *└────────────────────────────────────────────────┘
   **/
    public class BaseConfig
    {
        public static String FormatString = "yyyy-MM-dd HH:mm:ss";
        public static String localIp = "";
        public static String ipCode ="";
        public static String systemId = "";
        public static bool loggerMongon = true;
        public static bool loggerInfoLoggerFile = true;
        public static bool httpServerLog = true;
        public static bool httpClientLog = false;
        public static bool MyConsole = false;
        public static int signalLife =1000* 5;



        public static int tokenTime = -1;
        static BaseConfig()
        {
            Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
            JsonConvertSet();
            localIp = getLocalIP();
            DateTimeSerializationOptions.Defaults = DateTimeSerializationOptions.LocalInstance;
        }
        public static string getLocalIP()
        {
            try
            {
                 string HostName = Dns.GetHostName(); //得到主机名
                IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
                for (int i = 0; i < IpEntry.AddressList.Length; i++)
                {
                    //从IP地址列表中筛选出IPv4类型的IP地址
                    //AddressFamily.InterNetwork表示此IP为IPv4,
                    //AddressFamily.InterNetworkV6表示此地址为IPv6类型
                    IPAddress ip = IpEntry.AddressList[i];
                     if (ip.AddressFamily == AddressFamily.InterNetwork)
                    {
                        string ipSr = ip.ToString();
                        systemId = ConfigurationManager.AppSettings.Get("systemId");
                        int id = int.Parse(systemId);
                        long iplast = ip.Address >> 24 ;
                        if( iplast > 200 ) {
                            iplast -= id;
                        } else {
                            iplast += id;
                        }
                        ipCode = BaseUtil.to64(iplast);
                        return ipSr;
                    }
                }
                return "";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public static void JsonConvertSet()
        {
            Newtonsoft.Json.JsonSerializerSettings setting = new Newtonsoft.Json.JsonSerializerSettings();
            setting.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
            setting.DateFormatString = FormatString;
            setting.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            //空值处理
            setting.NullValueHandling = NullValueHandling.Ignore;
            setting.ContractResolver = new CamelCasePropertyNamesContractResolver();
            setting.Formatting = Formatting.Indented;
            //高级用法九中的Bool类型转换 设置
            setting.Error = (sender, args) =>
            {
                args.ErrorContext.Handled = true;
            };
            JsonConvert.DefaultSettings = new Func<JsonSerializerSettings>(() =>
            {
                return setting;
            });
        }
    }
}
